<?php

namespace app\index\controller;

use frame\lib\Db;
use frame\lib\VerfiyParam;
use PDOException;

class Config extends Base
{
    public function get()
    {
        /* 1显示视图 2 隐藏视图 默认隐藏视图 */
        $type = empty($_GET['type']) ? 1 : 2;
        $conf = Db::name('config')->where(['user_id' => $this->uid])->find()['conf'];
        $conf = json_decode($conf, true);
        if ($type == 1) {
            $this->view('index', ['config' => $conf]);
            die;
        }
        $get = @$_GET['get'];
        if (!empty($get)) {
            if (!array_key_exists($get, $conf)) error($get . '配置参数不存在');
            $conf = $conf[$get];
            exit($conf);
        }
        success($conf);
    }
    public function set()
    {
        $param = $_POST;
        $param['is_log'] = isset($param['is_log']) ? 1 : 0;
        $param['is_sign'] = isset($param['is_sign']) ? 1 : 0;
        $param['new_switch'] = isset($param['new_switch']) ? 1 : 0;
        $param['feedback'] = isset($param['feedback']) ? 1 : 0;
        $rule = [
            'is_log' => 'in:1,0',
            'is_sign' => 'in:1,0',
            'new_switch' => 'in:1,0',
            'sign_three' => 'int|gt:0',
            'sign_seven' => 'int|gt:0',
            'sign_fifteen' => 'int|gt:0',
            'reg_vip_time' => 'int',
            'log_send_score' => 'int',
            'reg_send_score' => 'int',
            'reg_vip_time' => 'int',
            'version' => 'int',
        ];
        VerfiyParam::init($param, $rule);
        $conf = Db::name('config')->where(['user_id' => $this->uid])->find()['conf'];
        $conf = json_decode($conf, true);
        $data = [
            'new' => @$param['new'] ?? $conf['new'],
            'update_new' => @$param['update_new'] ?? $conf['update_new'],
            'feedback' => @$param['feedback'] ?? $conf['feedback'],
            'is_log' => @$param['is_log'] ?? $conf['is_log'],
            'new_switch' => @$param['new_switch'] ?? $conf['new_switch'],
            'is_sign' => @$param['is_sign'] ?? $conf['is_sign'],
            'version' => @$param['version'] ?? $conf['version'],
            'log_send_score' => @$param['log_send_score'] ?? $conf['log_send_score'],
            'reg_vip_time' => @$param['reg_vip_time'] ?? $conf['reg_vip_time'],
            'downloadLink' => @$param['downloadLink'] ?? $conf['downloadLink'],
            'sign_three' => @$param['sign_three'] ?? $conf['sign_three'],
            'sign_seven' => @$param['sign_seven'] ?? $conf['sign_seven'],
            'sign_fifteen' => @$param['sign_fifteen'] ?? $conf['sign_fifteen'],
        ];
        $data = json_encode($data, JSON_UNESCAPED_UNICODE);
        $res = Db::name('config')->where(['user_id' => $this->uid])->update(['conf' => $data]);
        if ($res) success();
        error();
    }
   
}
